"""
xpath->库：lxml：解析本地文件
              ：解析网页文件（服务器响应的数据：response.read().decode("utf-8")）
"""
from lxml import etree
import urllib.request

# 解析本地：etree.parse()
# 解析网页文件：etree.HTML()

url = 'https://www.baidu.com/index.htm'
headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36 SLBrowser/9.0.3.5211 SLBChan/105"
}
my_request = urllib.request.Request(url=url, headers=headers)
response = urllib.request.urlopen(my_request)
content = response.read().decode("utf-8")
# print(content)

# 1. lxml解析服务器响应文件
tree = etree.HTML(content)

# 2. 获取想要的数据 tree.xpath()-->返回列表
lst = tree.xpath('//input[@id="su"]/@value')
print(lst[0])
